import 'package:fish_redux/fish_redux.dart';
import 'package:flutter/material.dart';
import '../../../app_files.dart';
import '../../../PublicSource/UserInfo/user_info_entity.dart';

import 'action.dart';
import 'state.dart';

Widget buildView(
    UserTypeViewState state, Dispatch dispatch, ViewService viewService) {
  UserInfoEntity userInfo = GlobalStore.store.getState().userInfo;

  Future<bool> _onWillPop() {
    return showDialog(
          context: viewService.context,
          builder: (context) => new AlertDialog(
            title: new Text('提示?'),
            content: new Text('请选择工作身份'),
            actions: <Widget>[
              new FlatButton(
                onPressed: () {
                  Navigator.of(context).pop(false);
                },
                child: new Text('确定'),
              ),
            ],
          ),
        ) ??
        false;
  }

  Widget workList() {
    List<Widget> items;
    if (state.isChanged) {
      items = userInfo.roleNames
          .map<Widget>((e) => InkWell(
                child: Container(
                  height: 49,
                  child: Row(
                    children: [
                      Text(
                        e,
                        style: TextThemeConfig.Font15WithColor22,
                      ),
                      Expanded(
                        child: Container(),
                      ),
                      Image.asset(state.selectIndex == e
                          ? 'images/cricle_select.png'
                          : 'images/circle.png'),
                    ],
                  ),
                  decoration: BoxDecoration(
                    border: Border(
                        bottom: BorderSide(color: ThemeConfig.separeColor)),
                    color: Colors.white,
                  ),
                ),
                onTap: () => dispatch(UserTypeViewActionCreator.onTapIndex(e)),
              ))
          .toList();
    } else
      items = userInfo.appSupportRoles
          .map<Widget>((e) => InkWell(
                child: Container(
                  height: 49,
                  child: Row(
                    children: [
                      Text(
                        e,
                        style: TextThemeConfig.Font15WithColor22,
                      ),
                      Expanded(
                        child: Container(),
                      ),
                      Image.asset(state.selectIndex == e
                          ? 'images/cricle_select.png'
                          : 'images/circle.png'),
                    ],
                  ),
                  decoration: BoxDecoration(
                    border: Border(
                        bottom: BorderSide(color: ThemeConfig.separeColor)),
                    color: Colors.white,
                  ),
                ),
                onTap: () => dispatch(UserTypeViewActionCreator.onTapIndex(e)),
              ))
          .toList();
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: items,
    );
  }

  Widget child = Scaffold(
    appBar: myAppBar(
      viewService.context,
      title: '角色选择',
    ),
    backgroundColor: Colors.white,
    body: Container(
      padding: EdgeInsets.only(
        bottom: 60,
        left: 15,
        right: 15,
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Container(
            height: 49,
            child: Align(
              alignment: Alignment.centerLeft,
              child: Text(
                '选择工作身份',
                style: TextThemeConfig.Font18WithColor22,
              ),
            ),
          ),
          workList(),
          Container(
            margin: EdgeInsets.only(top: 60),
            width: double.infinity,
            height: 50,
            decoration: BoxDecoration(
              color: ThemeConfig.mainColor,
              borderRadius: BorderRadius.all(Radius.circular(4)),
            ),
            child: InkWell(
              child: Center(
                child: Text(
                  '确定',
                  style: TextThemeConfig.Font16WithWhite,
                ),
              ),
              onTap: () => dispatch(UserTypeViewActionCreator.onSubmit()),
            ),
          )
        ],
      ),
    ),
  );

  if (state.isChanged) {
    return child;
  }

  return WillPopScope(
    onWillPop: _onWillPop,
    child: child,
  );
}
